package Game;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.AppGameContainer;


public class SimpleGame extends BasicGame implements MouseListener{

	private Image background = null;
	private Map map;
	private WaveHandler wh;
	private Tower marker = null;
	private MessageBar infoBar = null;
	private MenuBar menuBar = null;

	private Tower currentTower = null;





	private final static int WIDTH = 800;
	private final static int HEIGHT = 600;
	private final static int INFOBAR_TOP_HEIGHT = 50;
	private final static int INFOBAR_BOTTOM_HEIGHT = 100;
	private final static int LIVES = 20;


	public SimpleGame()
	{
		super("BirdDefense - An Epic Game");
	}

	/**
	 * Initiate the game.
	 */
	@Override
	public void init(GameContainer gc) throws SlickException {
		//Import the background image
		background = new Image("/data/background2.png");
		//Crate an empty map
		map = new Map();
		//Setup the map and create the WaveHandler
		wh = new Setup().setup(map);
		infoBar = new MessageBar();
		menuBar = new MenuBar(LIVES);

	}
	
	
	private class Mouse
	{
		boolean pressed;
		boolean invalidArea;
		boolean overMenu;
		int tX;
		int tY;
		
		Mouse(int x, int y, boolean pressed) throws SlickException
		{
			this.pressed = pressed;
			
			if (y <= INFOBAR_TOP_HEIGHT)
				invalidArea = true;
			if (y >= INFOBAR_TOP_HEIGHT + HEIGHT)
				overMenu = true;
			
			if (!invalidArea && !overMenu)
			{
				tX = getTile(x);
				tY = getTile(y);
			}
			else
			{
				tX = x;
				tY = y;
			}
			
		}

	}

	/**
	 * Update the game.
	 */
	@Override
	public void update(GameContainer gc, int delta) 
	throws SlickException     
	{
		Input input = gc.getInput();
		Mouse m = new Mouse(input.getMouseX(), input.getMouseY(), input.isMousePressed(0));
		
		if (!m.invalidArea && !m.overMenu && currentTower != null)
		{
			currentTower = new Tower(m.tX, m.tY);
		}
		
		if (m.pressed && !m.invalidArea && !m.overMenu)
		{
			marker = null;

			if (map.getEntity(m.tX, m.tY) instanceof Tower){
				infoBar.update("You clicked on a tower!");
				marker = (Tower) map.getEntity(m.tX, m.tY);

			} else if (map.getEntity(m.tX, m.tY) instanceof Path) {
				infoBar.update("You clicked on the path!");
			} else if(!map.isOccupied(m.tX, m.tY) && currentTower != null){

				map.addUnit(new Tower(m.tX, m.tY), m.tX, m.tY);	
				infoBar.update("A tower was sucsessfully added to the map!");
				currentTower = null;
			}
		}
		else if (m.pressed && m.overMenu)
		{
			if (menuBar.clickMenu(m.tX, m.tY) != null)
			{
				currentTower = new Tower(getTile(m.tX), getTile(m.tY));
			}
		}

		
		// This always happens!
		wh.attackWave(map.getTowers(), delta);	        
		wh.updateWave(delta);
		
		menuBar.update(LIVES, wh.getLeakedEnemies(), wh.getKills());


	}

	public int getTile(int coord){
		return coord/Map.TILESIZE;
	}

	/**
	 * Render the game on the screen.
	 */
	@Override
	public void render(GameContainer gc, Graphics g) 
	throws SlickException 
	{

		// Draw the background 
		background.draw(0,INFOBAR_TOP_HEIGHT);
		// Draw everything on the map
		map.drawMap();
		// Draw all enemies on the screen
		wh.drawWave();

		// Draws the info bar at the top of the screen
		infoBar.draw();
		
		menuBar.draw();
		
		// Draw the radius around a given tower
		if(marker != null)
			marker.drawRadius(g);

		// Draws the chosen tower
		if(currentTower != null && map != null)
		{
			if (map.getEntity(getTile(currentTower.getX()), getTile(currentTower.getY())) instanceof Path)
				currentTower.drawInvalid();
			else
				currentTower.drawTransparant();
		}


	}

	public static void main(String[] args) 
	throws SlickException
	{
		AppGameContainer app = new AppGameContainer(new SimpleGame());

		app.setDisplayMode(WIDTH, HEIGHT + INFOBAR_BOTTOM_HEIGHT + INFOBAR_TOP_HEIGHT, false);
		app.start();
	}

}
